<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  Module
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: mpCMSField.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 *  mpCMSField, a module helper class which handles CMS field data
 *
 * @author      Murat Purc <murat@purc.de>
 * @package     mpContenidoX
 * @subpackage  Module
 */
class mpCMSField
{

    /**
     * CMS field value
     * @var  mixed
     */
    protected $_value = null;

    /**
     * CMS field variable
     * @var  string
     */
    protected $_var = null;

    /**
     * CMS field variable
     * @var  string
     */
    protected $_properties = array();


    /**
     * Constructor, sets CMS field data
     *
     * @param   mixed   $value  CMS field value, e. g. CMS_VALUE[{n}] where {n} is a numeric identifier
     * @param   string  $var    CMS field variable, e. g. CMS_VAR[{n}] where {n} is a numeric identifier
     * @return  void
     */
    public function __construct($value='', $var='')
    {
        $this->setValue($value);
        $this->setVar($var);
    }


    /**
     * Returns the value as string representation of the instance
     *
     * @return  string
     */
    public function __toString()
    {
        return $this->getValue();
    }


    /**
     * Value setter
     *
     * @param   mixed  $value
     * @return  void
     */
    public function setValue($value)
    {
        $this->_value = $value;
    }


    /**
     * Value getter
     *
     * @return  mixed
     */
    public function getValue()
    {
        return $this->_value;
    }


    /**
     * Variable setter
     *
     * @param   string  $var
     * @return  void
     */
    public function setVar($var)
    {
        $this->_var = $var;
    }


    /**
     * Variable getter
     *
     * @return  string
     */
    public function getVar()
    {
        return $this->_var;
    }


    /**
     * Sets CMS field property data by name
     *
     * @param   string  $name   Property name
     * @param   mixed   $value  Property value
     * @return  void
     */
    public function setProperty($name, $value)
    {
        $this->_properties[$name] = $value;
    }


    /**
     * Gets CMS field property data by name
     *
     * @param   string  $name   Property name
     * @return  mixed   Property value
     * @throws  Exception if property doesn't exists by name
     */
    public function getProperty($name)
    {
        if (!isset($this->_properties[$name])) {
            throw new Exception('Access to not set property ' . $name);
        }
        return $this->_properties[$name];
    }


    public function __set($name, $value)
    {
        $this->setProperty($name, $value);
    }


    public function __get($name)
    {
        return $this->getProperty($name);
    }


    public function __isset($name)
    {
        return isset($this->_properties[$name]);
    }


    public function __unset($name)
    {
        if (isset($this->_properties[$name])) {
            unset($this->_properties[$name]);
        }
    }
}
